package com.lq.common;

import java.util.*;

/**
 * TreeNode
 *
 * @author qingqing
 * @date 2024/12/11
 */

public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    public TreeNode() {
    }

    public TreeNode(Integer val) {
        this.val = val;
    }

    public TreeNode(Integer val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }

    /**
     * @param root
     */
    public void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.println(root.val);
        preOrder(root.left);
        preOrder(root.right);
    }

    /**
     * @param root
     */
    public void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.println(root.val);
        inOrder(root.right);
    }

    /**
     * @param root
     */
    public void postOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        inOrder(root.right);
        System.out.println(root.val);
    }

    /**
     * @param root
     * @return
     */
    public List<Integer> preTraversal(TreeNode root) {
        List<Integer> result = new LinkedList<>();
        List<TreeNode> stack = new LinkedList<>();

        while (root != null || stack.size() != 0) {
            while (root != null) {
                stack.add(root);
                result.add(root.val);
                root = root.left;
            }
            root = stack.remove(stack.size() - 1);
            root = root.right;
        }
        return result;
    }

    /**
     * @param root
     * @return
     */
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> result = new LinkedList<>();
        List<TreeNode> stack = new LinkedList<>();

        while (root != null || stack.size() != 0) {
            while (root != null) {
                stack.add(root);
                root = root.left;
            }
            root = stack.remove(stack.size() - 1);
            result.add(root.val);
            root = root.right;
        }
        return result;
    }


    /**
     * @param root
     * @return
     */
    public List<Integer> postTraversal(TreeNode root) {
        List<Integer> result = new LinkedList<>();
        List<TreeNode> stack = new LinkedList<>();
        TreeNode lastNode = null;
        while (root != null || stack.size() != 0) {
            while (root != null) {
                stack.add(root);
                root = root.left;
            }
            TreeNode node = stack.get(stack.size() - 1);
            if (node.right == null || node.right == lastNode) {
                lastNode = stack.remove(stack.size() - 1);
                result.add(lastNode.val);
            } else {
                root = node.right;
            }
        }
        return result;
    }

    /**
     * 层序遍历
     *
     * @param root
     * @return
     */
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new LinkedList<List<Integer>>();
        Queue<TreeNode> queue = new LinkedList<>();

        if (root == null) {
            return res;
        }
        queue.offer(root);
        while (queue.size() != 0) {
            int n = queue.size();
            List<Integer> level = new LinkedList<>();
            for (int i = 0; i < n; i++) {
                TreeNode top = queue.poll();
                level.add(top.val);
                if (top.left != null) {
                    queue.offer(top.left);
                }
                if (top.right != null) {
                    queue.offer(top.right);
                }
            }
            res.add(level);
        }
        return res;
    }

    /**
     * 先序遍历和中序遍历生成树
     *
     * @param preorder
     * @param inorder
     * @return
     */
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        if (preorder.length == 0) {
            return null;
        }
        TreeNode root = new TreeNode(preorder[0]);
        if (preorder.length == 1) {
            return root;
        }
        int index = getIndex(inorder, preorder[0]);
        int[] leftInOrder = Arrays.copyOfRange(inorder, 0, index);
        int[] rightInOrder = Arrays.copyOfRange(inorder, index + 1, inorder.length);
        int[] leftPreOrder = Arrays.copyOfRange(preorder, 1, 1 + leftInOrder.length);
        int[] rightPreOrder = Arrays.copyOfRange(preorder, 1 + leftInOrder.length, preorder.length);
        root.left = buildTree(leftPreOrder, leftInOrder);
        root.right = buildTree(rightPreOrder, rightInOrder);
        return root;
    }

    public int getIndex(int[] inOrder, int target) {
        for (int i = 0; i < inOrder.length; i++) {
            if (target == inOrder[i]) {
                return i;
            }
        }
        return inOrder.length - 1;
    }

    /**
     * @param p
     * @param q
     * @return 返回两棵树是否相同
     */
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null && q == null) {
            return true;
        } else if (p == null || q == null) {
            return false;
        }
        return isSameTree(p.left, q.left) && isSameTree(p.right, q.right) && p.val == q.val;
    }


    /**
     * 判断树是否对称
     * 只需要判断树的左右两个子树是不是相反树
     *
     * @param root
     * @return
     */
    public boolean isSymmetric(TreeNode root) {
        return isSymmetricTree(root.left, root.right);
    }

    private boolean isSymmetricTree(TreeNode left, TreeNode right) {
        if (left == null && right == null) {
            return true;
        } else if (left == null || right == null) {
            return false;
        }
        return left.val == right.val && isSymmetricTree(left.left, right.right) && isSymmetricTree(left.right,
                right.left);
    }

    /**
     * 最大深度
     *
     * @param root
     * @return
     */
    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
    }

    /**
     * 锯齿遍历
     *
     * @param root
     * @return
     */
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> res = new LinkedList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        if (root == null) {
            return res;
        }
        queue.offer(root);
        int reverse = 0;
        while (queue.size() != 0) {
            int n = queue.size();
            Integer[] level = new Integer[n];
            for (int i = 0; i < n; i++) {
                TreeNode top = queue.poll();
                level[i] = top.val;
                if (top.left != null) {
                    queue.offer(top.left);
                }
                if (top.right != null) {
                    queue.offer(top.right);
                }
            }
            reverse++;
            if (reverse % 2 == 0) {
                for (int i = 0; i < n / 2; i++) {
                    int tmp = level[i];
                    level[i] = level[n - 1 - i];
                    level[n - 1 - i] = tmp;
                }
            }
            res.add(Arrays.asList(level));
        }
        return res;
    }

    /**
     * 从后序和中序构造二叉树
     *
     * @param inorder
     * @param postorder
     * @return
     */
    public TreeNode buildTree2(int[] inorder, int[] postorder) {
        if (postorder.length == 0) {
            return null;
        }

        TreeNode root = new TreeNode(postorder[postorder.length - 1]);
        if (postorder.length == 1) {
            return root;
        }
        int index = getIndex(inorder, postorder[postorder.length - 1]);
        int[] leftInorder = Arrays.copyOfRange(inorder, 0, index);
        int[] rightInorder = Arrays.copyOfRange(inorder, index + 1, inorder.length);
        int[] leftPostorder = Arrays.copyOfRange(postorder, 0, leftInorder.length);
        int[] rightPostorder = Arrays.copyOfRange(postorder, leftInorder.length, postorder.length - 1);
        root.left = buildTree2(leftInorder, leftPostorder);
        root.right = buildTree2(rightInorder, rightPostorder);
        return root;
    }

    /**
     * @param root
     * @return
     */
    public int minDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int depth = 0;
        if (root.left == null) {
            return minDepth(root.right) + 1;
        } else if (root.right == null) {
            return minDepth(root.left) + 1;
        }
        return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
    }

    /**
     * leetcode 112
     * 递归判断子树是否存在target = targetsum-root.val
     * 当参数为空时不存在树
     *
     * @param root
     * @param targetSum
     * @return
     */
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null) {
            return false;
        } else if (root.val == targetSum && root.left == null && root.right == null) {
            return true;
        } else if (root.val != targetSum && root.left == null && root.right == null) {
            return false;
        }
        return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val);
    }


    /**
     * @param root
     * @return
     */
    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }

        TreeNode left = invertTree(root.left);
        TreeNode right = invertTree(root.right);
        root.left = right;
        root.right = left;
        return root;
    }

    /**
     * leetcode113
     *
     * @param root
     * @param targetSum
     * @return
     */
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        List<List<Integer>> res = new LinkedList<>();
        List<Integer> path = new LinkedList<>();
        dfs1(root, targetSum, res, path);
        return res;
    }

    private void dfs1(TreeNode root, int targetSum, List<List<Integer>> res, List<Integer> path) {
        if (root == null) {
            return;
        } else if (root.left == null && root.right == null && targetSum == root.val) {
            path.add(root.val);
            res.add(new LinkedList<>(path));
            path.remove(path.size() - 1);
            return;
        } else if (root.left == null && root.right == null && targetSum != root.val) {
            return;
        }

        path.add(root.val);
        dfs1(root.left, targetSum - root.val, res, path);
        path.remove(path.size() - 1);

        path.add(root.val);
        dfs1(root.right, targetSum - root.val, res, path);
        path.remove(path.size() - 1);

    }

    /**
     * 二叉树展开为链表
     *
     * @param root
     */
    public void flatten(TreeNode root) {
        if (root == null) {
            return;
        }
        if (root.left != null) {
            TreeNode tmp = root.right;
            root.right = root.left;
            TreeNode p = root.left;
            root.left = null;
            while (p.right != null) {
                p = p.right;
            }
            p.right = tmp;
        }
        flatten(root.right);
    }

    public TreeNode findNode(TreeNode root, int val) {
        if (root == null) {
            return null;
        } else if (val > root.val) {
            return findNode(root.right, val);
        } else if (val < root.val) {
            return findNode(root.left, val);
        }
        return root;
    }
}